"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentTime = exports.config = exports.uploadFile = exports.dealDbMessage = void 0;
const node_sdk_1 = __importDefault(require("@cloudbase/node-sdk"));
const fs_1 = __importDefault(require("fs"));
const config_1 = __importDefault(require("./config"));
exports.config = config_1.default;
const dealDbMessage = (data, code = 0, message = "", others = {}) => {
    return Object.assign({ data,
        code,
        message }, others);
};
exports.dealDbMessage = dealDbMessage;
/**
 *
 * @param {*} file 上传的文件
 * @param {*} url 上传到服务器的路径
 * @returns
 */
const uploadFile = (file, url) => {
    const app = node_sdk_1.default.init(config_1.default.cloudConfig);
    return new Promise((resolve, reject) => {
        const ext = file.name.split(".").pop();
        app
            .uploadFile({
            cloudPath: `${url}/${Date.now() + "." + ext}`,
            fileContent: fs_1.default.createReadStream(file.path), //fs.createReadStream(file)
        })
            .then((res) => {
            app
                .getTempFileURL({
                fileList: [res.fileID],
            })
                .then((res) => {
                if (res.fileList.length > 0) {
                    // @ts-ignore
                    resolve(res.fileList[0].download_url);
                }
                else {
                    resolve(false);
                }
            });
        })
            .catch((err) => {
            console.log("err", err);
            resolve(false);
        });
    });
};
exports.uploadFile = uploadFile;
const getCurrentTime = (fmt, date) => {
    let ret;
    const opt = {
        "y+": date.getFullYear().toString(),
        "m+": (date.getMonth() + 1).toString(),
        "d+": date.getDate().toString(),
        "H+": date.getHours().toString(),
        "M+": date.getMinutes().toString(),
        "S+": date.getSeconds().toString(), // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"));
        }
    }
    return fmt;
};
exports.getCurrentTime = getCurrentTime;
